#-*- encoding: utf-8 -*-
import win32com.client 
from Misc.Utils import *

from DataAccess.QueryBlp import *

from datetime import date,timedelta

from ReportTool.HldReport import *
from ReportTool.PnLReport import *
from ReportTool.NAVReport import *
from ReportTool.MktValReport import *
from ReportTool.TradeReport import *
from ReportTool.RealizedPnL import *
from ReportTool.OnshoreHldReport import *

from Config.ReportConfig import *
	
def Penjing_Mid_Mth_Report(ref_date):

	nvpu_crt = query_latest_NAV(ref_date, ['SC028_NONUS'])
	nvpu_pre_1m = query_latest_NAV(end_of_mth(ref_date, -1), ['SC028_NONUS'])
	hld = query_hld(ref_date, portf_list=['SC028'])
	
	tplt_file = TPLT_PATH + 'Mid Month Data Update for Penjing.xls'
	xApp = win32com.client.Dispatch("Excel.Application")
	xBook = xApp.Workbooks.Open(tplt_file)
	xSht = xBook.Worksheets('Data Update')
	
	long_expo = 0
	short_expo = 0
	for elem in hld:
		if elem[2] == 'CASH' or elem[2] == 'REPO':
			continue
		elif elem[3] == 'LONG':
			long_expo += elem[12]
		else:
			short_expo += elem[12]
	
	xSht.Cells(9, 3).Value = ref_date
	xSht.Cells(11, 4).Value = nvpu_crt[0][2]/nvpu_pre_1m[0][2] - 1
	xSht.Cells(14, 4).Value = long_expo - short_expo
	xSht.Cells(15, 4).Value = long_expo + short_expo
	
	rept_file = REPT_PATH + 'Mid Month Data Update for Penjing_''' + ref_date.strftime("%Y%m%d") + '.xls'
	xBook.SaveAs(rept_file)
	xBook.Close()
	del xApp
	
	print 'info, penjing mid month report completed at ', ref_date.isoformat()